from starlette.authentication import requires
from fastapi import APIRouter, Request, HTTPException
from fastapi.responses import FileResponse

from pathlib import Path
import myClinic.handbook.handbook_of_differential_diagnosis as handbook

router = APIRouter()


@router.get("/handbook/doc")
@requires("authenticated")
async def get_handbook_doc(request: Request):
    return handbook.handbook_doc()


@router.get("/handbook/jpg/{filename}")
@requires("authenticated")
async def get_handbook_jpg(request: Request, filename: str):
    decision_trees_jpgs = handbook.handbook_jpg()
    if filename in decision_trees_jpgs.keys():
        path = Path(Path(__file__).parent.parent, 'handbook',
                    decision_trees_jpgs[filename])
        return FileResponse(path=path, media_type='image/jpeg')
    else:
        raise HTTPException(status_code=404, detail='File not found')


@router.get("/expert")
@requires("authenticated")
async def get_expert_system(request: Request):
    try:
        path = Path(Path(__file__).parent.parent, "dsm5_expert_system")
        path = path / "dsm5_expert_system.zip"
        return FileResponse(path=path, media_type='application/zip')
    except Exception:
        raise HTTPException(status_code=404, detail='File not found')
